In [1]:
import graphlab


A newer version of GraphLab Create (v1.7.1) is available! Your current version is v1.6.1.

You can use pip to upgrade the graphlab-create package. For more information see https://dato.com/products/create/upgrade.

Load image dataset


In [2]:
image_train = graphlab.SFrame('image_train_data/')
image_test = graphlab.SFrame('image_test_data/')


[INFO] This non-commercial license of GraphLab Create is assigned to sandipto.neogi@gmail.com and will expire on October 11, 2016. For commercial licensing options, visit https://dato.com/buy/.

[INFO] Start server at: ipc:///tmp/graphlab_server-9962 - Server binary: /Users/sandiptoneogi/anaconda/envs/dato/lib/python2.7/site-packages/graphlab/unity_server - Server log: /tmp/graphlab_server_1448031779.log
[INFO] GraphLab Server Version: 1.6.1

Explore the image data


In [3]:
graphlab.canvas.set_target('ipynb')

In [4]:
image_train['image'].show()


Train a classifier on raw pixels


In [5]:
raw_pixel_model = graphlab.logistic_classifier.create(image_train, target='label', 
                                                      features=['image_array'])


PROGRESS: Creating a validation set from 5 percent of training data. This may take a while.
          You can set ``validation_set=None`` to disable validation tracking.

PROGRESS: Logistic regression:
PROGRESS: --------------------------------------------------------
PROGRESS: Number of examples          : 1878
PROGRESS: Number of classes           : 4
PROGRESS: Number of feature columns   : 1
PROGRESS: Number of unpacked features : 3072
PROGRESS: Number of coefficients    : 9219
PROGRESS: Starting L-BFGS
PROGRESS: --------------------------------------------------------
PROGRESS: +-----------+----------+-----------+--------------+-------------------+---------------------+
PROGRESS: | Iteration | Passes   | Step size | Elapsed Time | Training-accuracy | Validation-accuracy |
PROGRESS: +-----------+----------+-----------+--------------+-------------------+---------------------+
PROGRESS: | 1         | 6        | 0.000018  | 3.396507     | 0.350905          | 0.338583            |
PROGRESS: | 2         | 8        | 1.000000  | 4.354234     | 0.363685          | 0.322835            |
PROGRESS: | 3         | 9        | 1.000000  | 4.889019     | 0.419595          | 0.362205            |
PROGRESS: | 4         | 10       | 1.000000  | 5.474645     | 0.430777          | 0.377953            |
PROGRESS: | 5         | 11       | 1.000000  | 6.020932     | 0.442492          | 0.433071            |
PROGRESS: | 6         | 12       | 1.000000  | 6.587189     | 0.463259          | 0.448819            |
PROGRESS: | 10        | 17       | 1.000000  | 9.245274     | 0.520767          | 0.511811            |
PROGRESS: +-----------+----------+-----------+--------------+-------------------+---------------------+

Prediction with raw pixel model


In [6]:
image_test[0:3]['image'].show()



In [7]:
image_test[0:3]['label']


Out[7]:
dtype: str
Rows: 3
['cat', 'automobile', 'cat']

In [8]:
raw_pixel_model.predict(image_test[0:3])


Out[8]:
dtype: str
Rows: 3
['bird', 'cat', 'bird']

Evaluate raw pixel model


In [9]:
raw_pixel_model.evaluate(image_test)


Out[9]:
{'accuracy': 0.4805, 'confusion_matrix': Columns:
 	target_label	str
 	predicted_label	str
 	count	int
 
 Rows: 16
 
 Data:
 +--------------+-----------------+-------+
 | target_label | predicted_label | count |
 +--------------+-----------------+-------+
 |  automobile  |       bird      |  127  |
 |     bird     |       bird      |  579  |
 |     bird     |       dog       |  179  |
 |     bird     |       cat       |  122  |
 |     cat      |       dog       |  302  |
 |     dog      |       dog       |  424  |
 |     dog      |    automobile   |  106  |
 |     cat      |       cat       |  275  |
 |     cat      |    automobile   |  165  |
 |     dog      |       bird      |  284  |
 +--------------+-----------------+-------+
 [16 rows x 3 columns]
 Note: Only the head of the SFrame is printed.
 You can use print_rows(num_rows=m, num_columns=n) to print more rows and columns.}

Can we improve using deep features


In [11]:
len(image_train)


Out[11]:
2005

In [ ]:
#deep_learning_model = graphlab.load_model('http://s3.amazonaws.com/GraphLab-Datasets/deeplearning/imagenet_model_iter45')

In [ ]:
#image_train['deep_features'] = deep_learning_model.extract_features(image_train)

In [12]:
# Deep features are already computed, use those instead of extracting it using the loaded model
image_train.head(1)


Out[12]:
id image label deep_features image_array
24 Height: 32 Width: 32 bird [0.242871761322,
1.09545373917, 0.0, ...
[73.0, 77.0, 58.0, 71.0,
68.0, 50.0, 77.0, 69.0, ...
[1 rows x 5 columns]

Train a classifier on deep features


In [13]:
deep_features_model = graphlab.logistic_classifier.create(image_train, target='label', 
                                                          features=['deep_features'])


PROGRESS: Creating a validation set from 5 percent of training data. This may take a while.
          You can set ``validation_set=None`` to disable validation tracking.

PROGRESS: WARNING: Detected extremely low variance for feature(s) 'deep_features' because all entries are nearly the same.
Proceeding with model training using all features. If the model does not provide results of adequate quality, exclude the above mentioned feature(s) from the input dataset.
PROGRESS: Logistic regression:
PROGRESS: --------------------------------------------------------
PROGRESS: Number of examples          : 1901
PROGRESS: Number of classes           : 4
PROGRESS: Number of feature columns   : 1
PROGRESS: Number of unpacked features : 4096
PROGRESS: Number of coefficients    : 12291
PROGRESS: Starting L-BFGS
PROGRESS: --------------------------------------------------------
PROGRESS: +-----------+----------+-----------+--------------+-------------------+---------------------+
PROGRESS: | Iteration | Passes   | Step size | Elapsed Time | Training-accuracy | Validation-accuracy |
PROGRESS: +-----------+----------+-----------+--------------+-------------------+---------------------+
PROGRESS: | 1         | 5        | 0.000132  | 3.310536     | 0.755392          | 0.634615            |
PROGRESS: | 2         | 9        | 0.250000  | 6.136136     | 0.767491          | 0.644231            |
PROGRESS: | 3         | 10       | 0.250000  | 7.026064     | 0.772225          | 0.653846            |
PROGRESS: | 4         | 11       | 0.250000  | 7.924714     | 0.780116          | 0.663462            |
PROGRESS: | 5         | 12       | 0.250000  | 8.862033     | 0.782220          | 0.653846            |
PROGRESS: | 6         | 13       | 0.250000  | 9.825676     | 0.793267          | 0.682692            |
PROGRESS: | 7         | 14       | 0.250000  | 10.813605    | 0.804314          | 0.711538            |
PROGRESS: | 8         | 15       | 0.250000  | 11.746326    | 0.827459          | 0.740385            |
PROGRESS: | 9         | 16       | 0.250000  | 12.809101    | 0.852709          | 0.730769            |
PROGRESS: | 10        | 17       | 0.250000  | 13.807400    | 0.866912          | 0.769231            |
PROGRESS: +-----------+----------+-----------+--------------+-------------------+---------------------+

Predict using deep features model


In [14]:
deep_features_model.predict(image_test[0:3])


Out[14]:
dtype: str
Rows: 3
['cat', 'automobile', 'cat']

In [15]:
deep_features_model.evaluate(image_test)


Out[15]:
{'accuracy': 0.78725, 'confusion_matrix': Columns:
 	target_label	str
 	predicted_label	str
 	count	int
 
 Rows: 16
 
 Data:
 +--------------+-----------------+-------+
 | target_label | predicted_label | count |
 +--------------+-----------------+-------+
 |  automobile  |       cat       |   14  |
 |     dog      |       cat       |  238  |
 |  automobile  |       dog       |   5   |
 |     cat      |       bird      |   87  |
 |     bird     |       dog       |   53  |
 |     dog      |       bird      |   54  |
 |     cat      |    automobile   |   37  |
 |     bird     |       cat       |  115  |
 |     dog      |    automobile   |   17  |
 |     dog      |       dog       |  691  |
 +--------------+-----------------+-------+
 [16 rows x 3 columns]
 Note: Only the head of the SFrame is printed.
 You can use print_rows(num_rows=m, num_columns=n) to print more rows and columns.}

In [ ]:


In [ ]:


In [ ]: